<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">
    <link rel="stylesheet" href="css/demo03.css">
</head>

<body>
<div id="app">
    <div class="header">
        <img src="img/tb.png" alt="">
        <ul>
            <li>网页无障碍</li>
            <li><i class="el-icon-chat-dot-round"></i>登录页面改进建议</li>
        </ul>
    </div>
    <div class="main">
        <img src="img/bj.jpg" alt="">
        <div class="user">
            <div class="name">注册</div>
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="ruleForm.username"></el-input>
                </el-form-item>
                <el-form-item label="姓名" prop="username">
                    <el-input v-model="ruleForm.name"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="pass">
                    <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="checkPass">
                    <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="验证码" prop="checkPass">
                    <el-input placeholder="请输入验证码" v-model="ruleForm.code">
                        <template slot="append">
                            <img :src="imgsrc" @click="repcode" height="20px" style="width: auto;height: auto">
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>

            </el-form>
        </div>
    </div>
</div>

</body>
<script src="js/vue.js"></script>
<script src="js/axios-0.18.0.js"></script>
<script src="element-ui/lib/index.js"></script>
<script src="js/js.cookie.min.js"></script>
<script>
    new Vue({
        el: '#app',
        data() {
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.password) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm: {
                    username: '',
                    name: '',
                    password: '',
                    checkPass: '',
                    code: ''
                },
                imgsrc: "user/code",//此处最前面不需要加斜杠"/"
                rules: {
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                        {min: 2, max: 25, message: '长度在 2 到 25 个字符', trigger: 'blur'}
                    ],
                    name: [
                        {required: true, message: '请输入姓名', trigger: 'blur'},
                        {min: 2, max: 25, message: '长度在 2 到 25 个字符', trigger: 'blur'}
                    ],
                    password: [
                        {validator: validatePass, trigger: 'blur'}
                    ],
                    checkPass: [
                        {validator: validatePass2, trigger: 'blur'}
                    ]
                }
            }
        },
        mounted() {
            this.ruleForm.username = Cookies.get("username")
            this.ruleForm.password = Cookies.get("password")
        },
        methods: {
            submitForm(formName) {
                var _this = this
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.post("/taobao/user/register", this.ruleForm)
                            .then(resp => {
                                if (resp.data == "succeed") {
                                    alert("注册成功,即将跳转主页面")
                                    window.location.href = "/taobao/main.html"
                                } else if (resp.data == "failed") {
                                    alert("该用户名已注册")
                                } else if (resp.data == "codefal") {
                                    alert("验证码错误")
                                    _this.repcode()//更换新的验证码
                                    _this.code = ''//输入的验证码清零
                                } else {
                                    alert("网络超时")
                                }
                            })
                    } else {
                        alert("请按规定格式输入");
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            repcode() {
                //    使参数不同，会认为是不同的验证码
                this.imgsrc = "user/code?time=" + new Date().getMilliseconds()//此处的imgsrc路径最前面也不需要加"/"
            }
        }
    })
</script>

</html>